import json
import random
from django.shortcuts import render

# Create your views here.
from user import userser, models
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from utils.MyRedis import myredis
from utils.sendmessage import send_message
from utils.tooljwt import tooljwt
from utils.authontication import JWTAuthenticationss

class UserRegister(APIView):
    def post(self,request):
        ser = userser.SerUser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"msg":"ok","data":ser.data,"code":status.HTTP_201_CREATED})
        return Response({"msg":"error","data":ser.errors,"code":status.HTTP_400_BAD_REQUEST})


#用户名和密码的登陆
class UsernameLogin(APIView):
    def post(self,request):
        #用户登录逻辑  用户名  密码
        username = request.data.get("username")
        password = request.data.get("password")
        data = models.UserModel.objects.filter(username=username,password=password).first()
        if data:
            #用户名和密码同时正确----登陆成功
            token = tooljwt.gettoken({"id":data.id,"username":data.username})
            return Response({"msg":"ok","data":{"id":data.id,"username":data.username,"token":token}})
        else:
            info = models.UserModel.objects.filter(username=username).first()
            if info:
                #用户名正确 密码错误
                return Response({"msg":"密码错误"})
            else:
                #用户名错误
                return Response({"msg":"用户名错误"})


class UserCenter(APIView):
    authentication_classes = (JWTAuthenticationss,)
    def get(self,request):
        return Response({"msg":"我是会员中心"})


class SendCode(APIView):
    def post(self,request):
        phone = request.data.get("phone")
        #生成验证码
        code = str(random.randint(1000,9999))
        res = json.loads(send_message(phone,code,2))
        if res['statusCode'] == "000000":
            myredis.set_time_str(phone,60*2,code)
            return Response({"msg":"发送成功"})
        else:
            return Response({"msg":res["statusMsg"]})

class Login(APIView):
    def post(self,request):
        phone = request.data.get("phone")
        code = request.data.get("code")
        #验证手机号是否正确
        data = models.UserModel.objects.filter(phone=phone).first()
        if data:
            #如果手机号正确再从redis读取验证码
            rediscode = myredis.get_str(phone)
            if code == rediscode:
                token=tooljwt.gettoken({"id": data.id})
                return Response({"msg":"登录成功","data": {"id":data.id,"token":token}})
            else:
                return Response({"msg":"验证码错误"})
        else:
            return Response({"msg":"手机号不存在"})


class ReLogin(APIView):
    def post(self,request):
        phone = request.data.get("phone")
        code = request.data.get("code")
        mycode=myredis.get_str(phone)
        if code == mycode:
            data=models.UserModel.objects.filter(phone=phone).first()
            if not data:
                data = models.UserModel.objects.create(phone=phone, username=phone, password='000000',email=phone + "@qq.com")
            token=tooljwt.gettoken({"id":data.id})
            return Response({"msg":"登录成功","data": {"id":data.id,"token":token}})
        else:
            return Response({"msg":"验证码错误"})
